<template>
    <el-dialog v-model="$store.state.HomeMoudule.addlistdata" title="请输入添加信息">
        <el-form :model="formadd">
            <el-form-item label="户主" :label-width="formLabelWidth">
                <el-input v-model="formadd.name" autocomplete="off" />
            </el-form-item>
            <el-form-item label="楼盘名称" :label-width="formLabelWidth">
                <el-input v-model="formadd.add" autocomplete="off" />
            </el-form-item>
            <el-form-item label="房源类型" :label-width="formLabelWidth">
                <el-input v-model="formadd.type" autocomplete="off" />
            </el-form-item>
            <el-form-item label="房源类型" :label-width="formLabelWidth">
                <el-input v-model="formadd.hometype" autocomplete="off" />
            </el-form-item>
            <el-form-item label="门牌号" :label-width="formLabelWidth">
                <el-input v-model="formadd.num" autocomplete="off" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="addcloseDialog(0)">取消</el-button>
                <el-button type="primary" @click="addcloseDialog(1)">
                    确定
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { useStore } from "vuex";
import Link from "../api/Link.js";
import apiurl from "../api/url.js";

const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
var time = new Date();
const formadd = reactive({
    name: '',
    add: "",
    type: "",
    hometype: "",
    num: "",
})

let date = new Date();
let myyear = date.getFullYear();
let mymonth = date.getMonth() + 1;
let myweekday = date.getDate();
let Daydata = myyear + '-' + mymonth + '-' + myweekday;


let store = useStore()

let addcloseDialog = async(numb: number) => {
    store.commit("ADD")
    if (numb == 1) {
        console.log(store.state.HomeMoudule.addlistData)
        await Link(apiurl.userlist, "POST",
            { id:Daydata ,name: formadd.name, title: formadd.add, type: formadd.type, hometype: formadd.hometype, num: formadd.num })
            .then((ok: any) => {
                console.log(ok)
                if (Object.keys(ok.data).length !== 0) {
                    ElMessage({
                        message: '注册成功！',
                        type: 'success',
                    })
                } else {
                    ElMessage.error('注册失败！')
                }
            })
        // 清空输入框内容
        for (let key in formadd) {
            formadd[key] = ''
        }
    }
    store.dispatch("USERUPDATA_LIST")
}

</script>

<style scoped>
.dialog-footer button:first-child {
    margin-right: 10px;
}
</style>